﻿var zombies = zombies || {};

zombies.AddMember = function () {
    var self = this;
    self.UserName = ko.observable();
    self.Password = ko.observable();
    self.FirstName = ko.observable();
    self.LastName = ko.observable();
    self.Gender = ko.observable("");
    self.Email = ko.observable();
    self.ConfirmPassword = ko.observable();

    self.doAdd = function (formElement) {
        var dataToAdd = {
            UserName: self.UserName,
            Password: self.Password,
            FirstName: self.FirstName,
            LastName: self.LastName,
            Gender: self.Gender,
            Email: self.Email
        };

        if ($(formElement).valid()) {
            $.ajax({
                type: "POST",
                url: "/api/AdminMember/AddNewMember",
                data: ko.toJSON(dataToAdd), //Convert the Observable Data into JSON
                contentType: "application/json",
                dataType: 'json'
            }).done(function () {
                toastr.success('Tạo Tài Khoản Thành Viên Thành Công!');
            }).fail(function (request) {
                var msg = request.responseText.substring(12);
                msg = msg.substring(0, msg.length - 2);
                toastr.error(msg);
            });
        }
    };
}

var viewModel;
$(document).ready(function () {

    viewModel = new zombies.AddMember();

    ko.applyBindings(viewModel, document.getElementById("admin_content"));

});